package cz.proks.easyhelpdesk.gwt.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import cz.proks.easyhelpdesk.api.model.Helpdesk;
import cz.proks.easyhelpdesk.gwt.event.CancelEvent;
import cz.proks.easyhelpdesk.gwt.event.HasCancelHandlers;
import cz.proks.easyhelpdesk.gwt.event.HasHelpdeskSaveHandlers;
import cz.proks.easyhelpdesk.gwt.event.HelpdeskSaveEvent;
import cz.proks.easyhelpdesk.gwt.event.HelpdeskSaveEvent.Handler;
import cz.proks.easyhelpdesk.gwt.ui.composite.RestrictedComposite;
import cz.proks.easyhelpdesk.gwt.ui.layout.MainLayoutPanel;
import cz.proks.easyhelpdesk.gwt.ui.widget.CancelButton;
import cz.proks.easyhelpdesk.gwt.ui.widget.SaveButton;

public class NewHelpdeskViewImpl extends RestrictedComposite implements NewHelpdeskView, HasHelpdeskSaveHandlers, HasCancelHandlers, HelpdeskSaveEvent.Handler, CancelEvent.Handler {

    private static NewHelpdeskViewImplUiBinder uiBinder = GWT.create(NewHelpdeskViewImplUiBinder.class);  
    
    private Presenter presenter;
        
    @UiField MainLayoutPanel mainLayoutPanel;
    @UiField TextBox helpdeskNameBox;
    @UiField SaveButton saveButton;
    @UiField CancelButton cancelButton;
    
    interface NewHelpdeskViewImplUiBinder extends UiBinder<Widget, NewHelpdeskViewImpl> {
    }

    public NewHelpdeskViewImpl() {
        initWidget(uiBinder.createAndBindUi(this));
    }
    
    @Override
    protected void initWidget(Widget widget) {     
        super.initWidget(widget);
        
        addHelpdeskSaveHandler(this);
        addCancelHandler(this);
    }
    
    @Override
    protected MainLayoutPanel getRootPanel() {
        return mainLayoutPanel;
    }

    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;        
    }
    
    @Override
    public String getHelpdeskName() {
        return helpdeskNameBox.getValue();
    }
    
    @Override
    public void setHelpdeskName(String helpdeskName) {
        this.helpdeskNameBox.setValue(helpdeskName);
    }        
    
    @Override
    public void onHelpdeskSave(HelpdeskSaveEvent event) {
        if (presenter != null)
            presenter.saveHelpdesk(event.getHelpdesk());        
    }
    
    @Override
    public void onCancel(CancelEvent event) {
        if (presenter != null)
            presenter.cancel();
    }

    @Override
    public HandlerRegistration addHelpdeskSaveHandler(Handler handler) {
        return addHandler(handler, HelpdeskSaveEvent.TYPE);
    }
    
    @Override
    public HandlerRegistration addCancelHandler(cz.proks.easyhelpdesk.gwt.event.CancelEvent.Handler handler) {
        return addHandler(handler, CancelEvent.TYPE);
    }
    
    @Override
    public void onErase() {
        helpdeskNameBox.setText("");
    }

    private Helpdesk getHeldpesk() {
        Helpdesk helpdesk = new Helpdesk();
        
        helpdesk.setName(getHelpdeskName());
        
        return helpdesk;
    }
        
           
    @UiHandler("saveButton")
    void onSaveButtonClick(ClickEvent event) {
        event.preventDefault();
        HelpdeskSaveEvent.fireEvent(this, getHeldpesk());                     
    }
    
    @UiHandler("cancelButton")
    void onCancelButtonClick(ClickEvent event) {
        event.preventDefault();
        CancelEvent.fireEvent(this);
    }
}
